#!/usr/bin/env python
# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ProductTemplate(models.Model):
    _inherit = 'product.template'

    requires_iqc = fields.Boolean(
        string='Requires IQC Inspection',
        help='Enable IQC inspection for this product'
    )
    reject_reason_line = fields.One2many('product.iqc.reject.reason.line', 'product_id', 'IQC Reject Reason Line')

class ProductCategory(models.Model):
    _inherit = 'product.category'

    s_requires_iqc = fields.Boolean(
        string='Requires IQC Inspection',
        help='Enable IQC inspection for this product category'
    )
    s_reject_reason_line = fields.One2many('product.iqc.reject.reason.line', 's_category_id', 'IQC Reject Reason Line')

class ProductIqcRejectReasonLine(models.Model):
    _name = 'product.iqc.reject.reason.line'

    product_id = fields.Many2one('product.template', 'Product ID', required=False)
    s_category_id = fields.Many2one('product.category', 'Category ID', required=False)
    reject_type = fields.Selection([('back','退回'),('reject','拒收')], 'Reject Type')
    reject_code = fields.Char('Reject Code')
    reject_node = fields.Char('Rejct Node')

    #创建产品拒收原因自动关联到产品分类
    #@api.model_create_multi
    #def create(self, vals_list):
    #    for vals in vals_list:
    #        product_id = vals.get('product_id', False)
    #        if product_id:
    #            product_row = self.env['product.template'].search([('id','=',product_id)])
    #            vals['s_category_id'] = product_row.categ_id.id if product_row.categ_id else False
    #    res = super().create(vals_list)
    #    return res

    @api.depends('product_id', 'reject_code', 'reject_type')
    def _compute_display_name(self):
        result = []
        for record in self:
            reject_type = dict(record._fields['reject_type'].selection).get(record.reject_type)
            display_name = f"{record.reject_code} - {record.reject_node}（{reject_type}）"
            record.display_name = display_name
